package top.wardyan.wtask.tapd.vo;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ReUtil;
import top.wardyan.common.util.DateUtil;

public class TapdMsg {

	@SuppressWarnings("unused")
	private static final cn.hutool.log.Log log = cn.hutool.log.Log.get();
	
	String from;
	Timestamp date;

	List<TapdMsgLine> lines;
	
	
	public static TapdMsg parse(String msg) {
		TapdMsg rs = new TapdMsg();
		
		// 消息格式:from=冯超华&msg=2024-11-23 周六
		// 兴雪莱 - 【ID1025698】 （加急）天泽吊挂对接（吊挂线号区分） 2 未完成
		// 兴雪莱 - 【ID1025803】 扎卡生成规则调整 2.5
		// 兴雪莱 - 【ID1025460】 pda收料赋码增加公司限制 1
		// ReUtil.findAll("", msg);
		// ReUtil.getAllGroups("", msg);
		Pattern pattern = Pattern.compile("from=(.*)&msg=(.*)", Pattern.DOTALL);
		List<String> lines = ReUtil.getAllGroups(pattern, msg, false);
		// lines:
		// 1姓名
		// 2日期等

		if(lines == null) {
			lines = new ArrayList<>();
		}
		
		if(lines != null && lines.size() > 1) {
			
			rs.setFrom(lines.get(0));
			String msglines = lines.get(1);
			if(msglines == null) {
				msglines = "";
			}
			
//			Pattern linepaggern = Pattern.compile("from=(.*)&msg=(.*)", Pattern.DOTALL);
//			List<String> tasklines = ReUtil.getAllGroups(linepaggern, msglines, false);
			String[] tasklinestrs = msglines.split("\n");
			
			if(tasklinestrs != null && tasklinestrs.length > 0) {
				String firstline = tasklinestrs[0];
				String[] fs = firstline.split(" ");		//有些日期行会带星期 说的就是我
				if(fs != null && fs.length > 0) { 
					firstline = fs[0];
				}
				rs.setDate(DateUtil.parse(firstline));
				
				tasklinestrs = ArrayUtil.sub(tasklinestrs, 1, tasklinestrs.length + 10);
			}
			List<TapdMsgLine> gns = new ArrayList<>();
			
			for(String line : tasklinestrs) {
				TapdMsgLine gn = TapdMsgLine.parse(line);
				gns.add(gn);
			}
			rs.setLines(gns);
		}
		
		return rs;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	public String getFrom() {
		return from;
	}

	public TapdMsg setFrom(String from) {
		this.from = from;
		return this;
	}

	public List<TapdMsgLine> getLines() {
		return lines;
	}

	public TapdMsg setLines(List<TapdMsgLine> lines) {
		this.lines = lines;
		return this;
	}

	public Timestamp getDate() {
		return date;
	}

	public TapdMsg setDate(Timestamp date) {
		this.date = date;
		return this;
	}
	
	
	
	
}
